package com.yuma.tingshu.order.template;

import com.yuma.tingshu.common.constant.SystemConstant;
import com.yuma.tingshu.common.result.Result;
import com.yuma.tingshu.model.user.VipServiceConfig;
import com.yuma.tingshu.order.template.anno.TradBean;
import com.yuma.tingshu.user.client.VipServiceConfigFeignClient;
import com.yuma.tingshu.vo.order.OrderDerateVo;
import com.yuma.tingshu.vo.order.OrderDetailVo;
import com.yuma.tingshu.vo.order.OrderInfoVo;
import com.yuma.tingshu.vo.order.TradeVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import java.math.BigDecimal;
import java.util.List;

@TradBean(value = SystemConstant.ORDER_ITEM_TYPE_VIP)
public class VipTrade extends TradeTemplate{

    @Resource
    private VipServiceConfigFeignClient vipServiceConfigFeignClient;

    @Override
    public void buildOrderInfoVo(OrderInfoVo orderInfoVo, TradeVo tradeVo) {
        // 5.远程调用vipServiceConfig接口，根据itemId->查询VSC对象
        Result<VipServiceConfig> vipServiceConfigResult = vipServiceConfigFeignClient.getVipServiceConfig(tradeVo.getItemId());
        // 6.判空
        Assert.notNull(vipServiceConfigResult, "生成VIP订单，根据vip配置id查询VIP配置详情");
        VipServiceConfig vipServiceConfig = vipServiceConfigResult.getData();
        Assert.notNull(vipServiceConfig, "生成VIP订单，根据vip配置id查询VIP配置详情");
        // 7.获取Result数据，给原价，减免，总额赋值
        orderInfoVo.setOriginalAmount(vipServiceConfig.getPrice().setScale(2));
        orderInfoVo.setDerateAmount(vipServiceConfig.getPrice().subtract(vipServiceConfig.getDiscountPrice()).setScale(2));
        orderInfoVo.setOrderAmount(vipServiceConfig.getDiscountPrice().setScale(2));

        // 8.封装订单详情集合
        orderInfoVo.setOrderDetailVoList(List.of(
                new OrderDetailVo()
                        .setItemId(vipServiceConfig.getId())
                        .setItemName("VIP:" + vipServiceConfig.getName())
                        .setItemPrice(vipServiceConfig.getDiscountPrice())
                        .setItemUrl(vipServiceConfig.getImageUrl())
        ));
        // 9.封装订单减免集合
        // 判断减免金额是否大于0，大于0才有减免详情
        if (orderInfoVo.getDerateAmount().compareTo(BigDecimal.ZERO) > 0) {
            orderInfoVo.setOrderDerateVoList(List.of(
                    new OrderDerateVo()
                            .setDerateType(SystemConstant.ORDER_DERATE_VIP_SERVICE_DISCOUNT)
                            .setDerateAmount(orderInfoVo.getDerateAmount())
                            .setRemarks("VIP购买:" + vipServiceConfig.getName())
            ));
        }
    }
}
